from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class LoginPage:
    """登录页面封装"""

    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 10)

    #定位器
    USERNAME_INPUT = (By.CLASS_NAME, "el-input__inner")
    PASSWORD_INPUT = (By.XPATH, '//*[@id="app"]/div[1]/div[1]/div[2]/form/div[2]/div/div[1]/input')
    LOGIN_BUTTON = (By.XPATH, "//button//span[text()='登录']")

    def open_login_page(self,url):
        """打开登录页面"""
        self.driver.get(url)

    def enter_username(self,username):
        """输入用户名"""
        username_field = self.wait.until(
            EC.presence_of_element_located(self.USERNAME_INPUT)
        )
        username_field.clear()
        username_field.send_keys(username)

    def enter_password(self,password):
        """输入密码"""
        password_field = self.wait.until(
            EC.presence_of_element_located(self.PASSWORD_INPUT)
        )
        password_field.clear()
        password_field.send_keys(password)

    def click_login(self):
        """点击登录按钮"""
        login_btn = self.wait.until(
            EC.element_to_be_clickable(self.LOGIN_BUTTON)
        )
        login_btn.click()

    def login(self,username,password):
        """完整的登录流程"""
        self.enter_username(username)
        self.enter_password(password)
        self.click_login()

